package com.dwx.guli.service.vod.controller.admin;

import com.aliyuncs.exceptions.ClientException;
import com.dwx.guli.service.base.exception.GuliException;
import com.dwx.guli.service.base.result.R;
import com.dwx.guli.service.base.result.ResultCodeEnum;
import com.dwx.guli.service.vod.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Slf4j
@Api(tags = "阿里云视频点播")
@CrossOrigin
@RestController
@RequestMapping("/admin/vod/media")
public class MediaController {
    @Autowired
    VideoService videoService;
    @PostMapping("/upload")
    public R upload( @ApiParam(name = "file", value = "文件", required = true)
                     @RequestParam("file") MultipartFile file){
        try {
            String originalFilename = file.getOriginalFilename();
            String videoId = videoService.uploadVideo(file.getInputStream(), originalFilename);
            return R.ok().data("videoId", videoId).message("视频上传成功");
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
            throw new GuliException(ResultCodeEnum.VIDEO_UPLOAD_TOMCAT_ERROR);
        }
    }
    @ApiOperation("删除视频")
    @DeleteMapping("remove/{videoSourceId}")
    public R remove(
            @ApiParam(value="阿里云视频id", required = true)
            @PathVariable String videoSourceId){
        try {
            videoService.removeVideo(videoSourceId);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
            throw new GuliException(ResultCodeEnum.VIDEO_DELETE_ALIYUN_ERROR);
        }
    }


    @ApiOperation("删除视频")
    @DeleteMapping("remove")
    public R removeVideoByIdList(
            @ApiParam(value="阿里云视频id", required = true)
            @RequestBody List<String > videoIdList){
        try {
            videoService.removeVideoByIdList(videoIdList);
            return  R.ok().message("视频删除成功");
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
            throw new GuliException(ResultCodeEnum.VIDEO_DELETE_ALIYUN_ERROR);
        }
    }
}
